/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wat.serwerapp.main;

/**
 *
 * @author Sebateee
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.*;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import wat.serwerapp.connection.MessageHandler;
import wat.ziszcommon.transport.EprOpenSslContextFactory;
//import LDAP.*;

public class Server {

    public static final int PORT = 7000;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        SocketAcceptor acceptor = new NioSocketAcceptor();
        acceptor.setReuseAddress(true);

        if(Parameters.getInstance().isUseSSL()) {
            EprOpenSslContextFactory sslContextFactory = EprOpenSslContextFactory.getInstance(true);
            InputStream certFileStream = Server.class.getResourceAsStream(Parameters.getInstance().getSslCertFile());
            sslContextFactory.setCertFile(certFileStream);
            sslContextFactory.setCertPassword(Parameters.getInstance().getSslCertPassword());
            SslFilter sslFilter = new SslFilter(sslContextFactory.getSSLContext());
            acceptor.getFilterChain().addLast("sslFilter", sslFilter);
            System.out.println("SSL on");
        }
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        acceptor.setHandler(new MessageHandler());
        acceptor.bind(new InetSocketAddress(PORT));


        System.out.println("Listening on port " + PORT);

        for (;;) {
            System.out.println("R: " + acceptor.getStatistics().getReadBytesThroughput() +
                    ", W: " + acceptor.getStatistics().getWrittenBytesThroughput());
            Thread.sleep(3000);
        }

        //LdapCon ldap;
        /*
        ServerSocket nasluch = new ServerSocket(PORT);
        System.out.println("Czekam na zgloszenia klientow");

        try {
            while (true) {
                Socket polaczenie = nasluch.accept();
                System.out.printf("Zgloszenie Klienta - " + polaczenie.getRemoteSocketAddress());

                try {
                    new ObslugaKlienta(polaczenie);
                } catch (IOException e) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, e);
                    polaczenie.close();
                }
            } //end while
        } finally {
        }*/
    }
}
